﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Core.DataAccess.IRepositories;

namespace Core.DataAccess.Repositories
{
    [Export(typeof(IBlogRepository))]
    public class BlogRepository : IBlogRepository
    {
        private Connection conn;

        public BlogRepository()
        {
            conn = new Connection();
        }

        public void SaveBlog(Blog blog)
        {
            using (MXHEntities context = new MXHEntities())
            {
                if (blog.BlogID > 0)
                {
                    context.Blogs.Attach(new Blog { BlogID = blog.BlogID });
                    context.Blogs.ApplyCurrentValues(blog);
                }
                else
                {
                    context.Blogs.AddObject(blog);
                }
                context.SaveChanges();
            }
        }

        public List<Blog> GetBlogListByAccountID(Int32 accountID)
        {
            using (MXHEntities context = new MXHEntities())
            {
                //return context.Blogs.Where(b => b.Account.Equals(accountID)).OrderByDescending(t => t.UpdateDate).ToList();
                return context.Blogs.OrderByDescending(t => t.UpdateDate).ToList();
            }
        }

        public List<Blog> GetBlogListBySubject(string subject)
        {
            using (MXHEntities context = new MXHEntities())
            {
                return context.Blogs.Where(b => b.Subject.Equals(subject)).ToList();
            }
        }

        public Blog GetBlogByBlogID(Int32 blogID)
        {
            using (MXHEntities context = new MXHEntities())
            {
                return context.Blogs.Where(b => b.BlogID.Equals(blogID)).SingleOrDefault();
            }
        }
    }
}
